package com.server.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.server.date.DateType;
import com.server.date.DateUtil;
import com.server.service.IForeignService;
import com.server.service.IUserAssignmentService;
@Service("ForeignService")
public class ForeignService implements IForeignService{
	
	private String tableName=null;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IUserAssignmentService userAssignmentService;

	@Override
	public Integer deleteC(String ids, String operator,String type) {
		switch (type) {
		case "1"://涉外国别
			tableName = "education.sicfa_con_reg_sit";
			break;
		case "2"://涉外机构和人员
			tableName = "education.sicfa_for_ins_per";
			break;
		case "3"://应急处理
			tableName = "education.sicfa_emr_hand";
			break;
		case "4"://相关专题
			tableName = "education.sicfa_rel_sub";
			break;
		default:
			break;
		}
		String sql = "delete  from %s where id in('%s')";
		return jdbcTemplate.update(String.format(sql, tableName,ids.replaceAll(",", "','")));
	}

	@Override
	public Integer toEstimateC(String ids, String operator,String type) {
		StringBuffer idsBuffer = new StringBuffer();
		Boolean estimateAssi = false;
		String[] split = ids.split(",");
		for(String id : split){
			switch (type) {
			case "1":
				estimateAssi = userAssignmentService.estimateAssi(Integer.parseInt(id), 5, 1);
				tableName = "education.sicfa_con_reg_sit";
				break;
			case "2":
				estimateAssi = userAssignmentService.estimateAssi(Integer.parseInt(id), 6, 1);
				tableName = "education.sicfa_for_ins_per";
				break;
			case "3":
				estimateAssi = userAssignmentService.estimateAssi(Integer.parseInt(id), 7, 1);
				tableName = "education.sicfa_emr_hand";
				break;
			case "4":
				estimateAssi = userAssignmentService.estimateAssi(Integer.parseInt(id), 8, 1);
				tableName = "education.sicfa_rel_sub";
				break;
			default:
				break;
			}
			
			if (estimateAssi) {
				if (idsBuffer.length() > 0) {
					idsBuffer.append(",");
				}
				idsBuffer.append(id);
			}
		}
		if (idsBuffer.length() <= 0) {
			return 0;
		}
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS);
		String sql = "update %s set state=1,mender='%s',updateTime='%s' where id in('%s')";
		
		return jdbcTemplate.update(String.format(sql, tableName,operator,dateStr,idsBuffer.toString().replaceAll(",", "','")));
	}

	@Override
	public Integer getId(Object uuid) {
		tableName = "education.sicfa_rel_sub";
		String sql = "select id from %s where uuid='%s'";
		Integer id = jdbcTemplate.queryForObject(String.format(sql, tableName,uuid.toString()), Integer.class);
		return id;
	}
	@Override
	public boolean updateState(String id, int state, String operator,String type,Boolean isAudit) {
		boolean isSuccess = true;
		String sql = null;
		if (isAudit) {
			sql = "update %s set state=%s,auditor='%s',auditTime='%s' where id=%s";
		}else {
			sql = "update %s set state=%s,evaluator='%s',evalueTime='%s' where id=%s";
		}
		String dateStr = DateUtil.getDateStr(new Date(),DateType.YYYY_MM_DD_HH_mm_ss_SSS);
		switch (type) {
		case "1"://涉外国别
			tableName = "education.sicfa_con_reg_sit";
			break;
		case "2"://涉外机构和人员
			tableName = "education.sicfa_for_ins_per";
			break;
		case "3"://应急处理
			tableName = "education.sicfa_emr_hand";
			break;
		case "4"://相关专题
			tableName = "education.sicfa_rel_sub";
			break;
		default:
			break;
		}
		System.out.println(String.format(sql, tableName,state,operator,dateStr,id));
		try {
			int update = jdbcTemplate.update(String.format(sql, tableName,state,operator,dateStr,id));
			if(update <= 0){
				isSuccess = false;
			}
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	@Override
	public List<Map<String, Object>> getEvalue( Integer id,
			Integer typeId) {
		String sql = "select * from education.evalue where relaId=%d and type=%d";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql,id,typeId));
		if(list.size()>0){
			return list;
		}
		return null;
	}
	@Override
	public List<Map<String, Object>> getAudit(Integer id,
			Integer typeId) {
		String sql = "select * from education.audit where relaId=%d and type=%d";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql,id,typeId ));
		if(list.size()>0){
			return list;
		}
		return null;
	}

}
